package com.cam.framework.utils;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class CustomExceptionHandler {

    public void commonHandle(Exception ex) {
        ex.printStackTrace();
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<RestResult> handle(HttpMessageNotReadableException ex) {
        this.commonHandle(ex);
        return new ResponseEntity<>(RestResult.error("数据解析失败"), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<RestResult> handle(Exception ex) {
        this.commonHandle(ex);
        return new ResponseEntity<>(RestResult.error("内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
